import Vue from "vue";
import { Message } from 'element-ui'
import VueRouter from "vue-router";
import App from "@/App.vue";

import Login from "@p/login/index.vue";
import Register from "@p/register/index.vue";
import Agreement from "@p/agreement/index.vue";
import AgreementRegister from '@p/agreement/register.vue'

import PayMethod from "@p/pay/payMethod.vue";
import PayInfo from "@p/pay/payInfo.vue";

import RepReim from '@p/errands/index.vue'
import RepReimHome from '@p/errands/wallet/home/index.vue'
import NewHome from '@p/errands/wallet/index/index.vue'
import Balance from '@p/errands/wallet/balance.vue'
import MyBalance from '@p/errands/wallet/mybalance.vue'
import Withaccount from '@p/errands/wallet/withaccount.vue'

// 代报员个人中心
import Person from "@p/errands/person/person.vue";
import EditUserInfo from "@p/errands/person/components/edituserinfo.vue";
import EditUserPassword from "@p/errands/person/components/edituserpassword.vue";
import EditUserPhone from "@p/errands/person/components/edituserphone.vue";
import ForgetPassword from "@p/errands/person/components/forgetPassword.vue";
import authentication from "@p/errands/person/components/authentication.vue";

import User from "@p/user/index.vue";
import UserHome from "@p/user/home/index.vue";
import UserHomeDetails from "@p/user/home/details/_id.vue";
import UserHomeSupplement from "@p/user/home/supplement.vue";
import UserApply from "@p/user/apply.vue";
import UserSet from "@p/user/set/index.vue";

import UserEditInfo from "@p/user/set/editInfo.vue";
import UserEditPassWord from "@p/user/set/editPassWord.vue";
import UserEditPhone from "@p/user/set/editPhone.vue";
import UserEditOA from "@p/user/set/setOAAccount.vue";

Vue.use(VueRouter);

const routes = [{
        path: "",
        name: "App",
        component: App,
        redirect: "/login",
        meta: {
            title: "我爱报销"
        }
    },
    {
        path: "/login", // 首页
        name: "Login",
        component: Login,
        meta: {
            title: "用户登录"
        }
    },
    {
        path: "/agreement", // 首页
        name: "Agreement",
        component: Agreement,
        meta: {
            title: "用户协议"
        }
    },
    {
        path: "/agreement/register", // 首页
        name: "AgreementRegister",
        component: AgreementRegister,
        meta: {
            title: "注册协议"
        }
    },
    {
        path: "/user", // 用户首页
        name: "User",
        component: User,
        redirect: "/user/home",
        children: [{
                path: "/user/home", //
                name: "UserHome",
                component: UserHome,
                meta: {
                    title: "首页",
                    requiresAuth: true
                }
            },
            {
                path: "/user/home/details/:id", //
                name: "UserHomeDetails ",
                component: UserHomeDetails,
                meta: {
                    title: "详情",
                    requiresAuth: true
                }
            },
            {
                path: "/user/home/supplement", //
                name: "UserHomeSupplement ",
                component: UserHomeSupplement,
                meta: {
                    title: "补充订单",
                    requiresAuth: true
                }
            },
            {
                path: "/user/apply", //
                name: "UserApply",
                component: UserApply,
                meta: {
                    title: "订单提交",
                    requiresAuth: true
                }
            },
            {
                path: "/user/set", //
                name: "UserSet",
                component: UserSet,
                meta: {
                    title: "个人中心",
                    requiresAuth: true
                }
            },
            {
                path: "/user/set/editInfo", //
                name: "UserEditInfo",
                component: UserEditInfo,
                meta: {
                    title: "修改资料",
                    requiresAuth: true
                }
            },
            {
                path: "/user/set/editPassWord", //
                name: "UserEditPassWord",
                component: UserEditPassWord,
                meta: {
                    title: "修改密码",
                    requiresAuth: true
                }
            },
            {
                path: "/user/set/editPhone", //
                name: "UserEditPhone",
                component: UserEditPhone,
                meta: {
                    title: "修改手机号",
                    requiresAuth: true
                }
            },
            {
                path: "/user/set/setOAAccount", //
                name: "UserEditOA",
                component: UserEditOA,
                meta: {
                    title: "修改OA账号",
                    requiresAuth: true
                }
            }
        ]
    },
    {
        path: "/pay/payMethod",
        name: "PayMethod",
        component: PayMethod,
        meta: {
            title: "支付方式"
        }
    },
    {
        path: "/pay/payInfo",
        name: "PayInfo",
        component: PayInfo,
        meta: {
            title: "确认支付",
            requiresAuth: true
        }
    },
    {
        path: "/register",
        name: "Register",
        component: Register,
        meta: {
            title: "用户注册"
        }
    },
    {
        path: "/person",
        name: "person",
        component: Person,
        meta: {
            title: "个人中心",
            requiresAuth: true
        }
    },
    {
        path: "/edituserinfo",
        name: "EditUserInfo",
        component: EditUserInfo,
        meta: {
            title: "修改资料",
            requiresAuth: true
        }
    },
    {
        path: "/authentication",
        name: "authentication",
        component: authentication,
        meta: {
            title: "实名认证",
            requiresAuth: true
        }
    },
    {
        path: "/edituserpassword",
        name: "EditUserPassword",
        component: EditUserPassword,
        meta: {
            title: "修改密码",
            requiresAuth: true
        }
    },
    {
        path: "/edituserphone",
        name: "EditUserPhone",
        component: EditUserPhone,
        meta: {
            title: "修改手机号",
            requiresAuth: true
        }
    },
    {
        path: "/forgetPwd",
        name: "ForgetPassword",
        component: ForgetPassword,
        meta: {
            title: "忘记密码",
        }
    },
    {
        path: "/replace",
        name: "Replace",
        redirect: "/replace/home",
        component: RepReim,
        meta: {
            title: "代报员"
        },
        children: [{
                path: '/replace/home',
                name: 'ReplaceHome',
                component: RepReimHome,
                meta: {
                    title: "首页",
                    // noKeepAlive: true
                }
            },
            {
                path:'/replace/new',
                name:'newHome',
                component:NewHome,
                meta:{
                    title:'新首页'    
                }
            },
            {
                path: '/replace/balance',
                component: Balance,
                name: 'Balance',
                meta: {
                    title: '提现'
                }
            },
            {
                path: '/replace/mybalance',
                component: MyBalance,
                name: 'MyBalance',
                meta: {
                    title: '我的余额'
                }
            },
            {
                path: '/replace/withaccount',
                component: Withaccount,
                name: 'withaccount',
                meta: {
                    title: '提现账户'
                }
            },
        ]
    },

];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes
});

router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面 title */
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!sessionStorage || !sessionStorage.getItem("token")) {
            console.log('xxx')
            Message.error("您还没有登录，请先登录");
            next("/login");
        } else {
            next();
        }
    } else {
        next();
    }

    next();
});

export default router;